import Vue from 'vue'
import Vuex from 'vuex'

import router from "../router/index";

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    routerlist: [{
      name: "首页",
      path: "/home",
      type: "success"
    }]
  },
  mutations: {
    addrouterlist(state, payload) {
      // 判断是否重复
      if (state.routerlist.length == 0) {
        state.routerlist.push(payload)
        this.commit("changecolor", payload)
      } else {
        let s = state.routerlist.find(item => {
          return item.path === payload.path
        })
        // 找的到
        if (s) {
          this.commit("changecolor", payload)
          return
        } else {
          // 找不到
          state.routerlist.push(payload)
          this.commit("changecolor", payload)
        }
      }
      console.log('routerlist', state.routerlist)
    },
    // 删除标签
    remove(state, index) {
      console.log("index", index)
      state.routerlist.splice(index, 1)
      if (state.routerlist.length == 0) {
        state.routerlist.push({
          name: "首页",
          path: "/home",
          type: "success"
        })
        router.push('/home')
      } else {
        // 跳到上一级路由
        let len = state.routerlist.length - 1
        let path = state.routerlist[len].path
        router.push(path)
      }
    },
    // 处理标签颜色
    changecolor(state, item1) {
      console.log("处理路径颜色", item1.path)
      state.routerlist.forEach(item => {
        if (item.path == item1.path) {
          item.type = "success"
        } else {
          item.type = ""
        }
      });
    }
  },
  actions: {},
  modules: {}
})